package org.itboys.mobile.service.mongo.site;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.web.SundayPermissionService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.itboys.mobile.util.Md5UtilJx;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/21
 * 描述：搜鸟私厨_前端_用户相关
 */
@Service
public class SundaySiteMemberService extends BaseMongoService<SundayMember>{
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Autowired
    private JoinSiteValidateCodeService codeService;
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayPermissionService permissionService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Autowired
    private SundayProductCategoryService categoryService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayMember> getEntityClass() {
        return SundayMember.class;
    }

    /**
     * 前端注册
     * @param userType
     * @param userName
     * @param password
     * @param nickName
     * @param brandName
     * @param businessType
     * @param categoryId
     * @param categoryName
     * @param mobileName
     * @param mobile
     * @param phone
     * @param code
     */
/*    @Transactional
    public void save(Integer userType,String userName,String password,String nickName,String brandName,String businessType,
                         Long categoryId,String categoryName,String mobileName,String mobile,String phone,String code){
        Map<String,Object> param = new HashMap<String, Object>();
        //1,校验验证码
        codeService.checkValidateCode(phone, code);
        checkMemberIsExist(phone,userName,0l);
        SundayProduct product=new SundayProduct();
        SundayProductCategory parent=categoryService.find(categoryService.find(categoryId).getParentId());
        product.setCategoryId(categoryId);
        product.setCategoryName(categoryName);
        product.setParentId(parent.getId());
        product.setParentName(parent.getName());
        SundayMember member=new SundayMember();
        member.setUserName(userName);
        member.setPassword(Md5UtilJx.GetMD5Code(password));
        member.setNickName(nickName);
        member.setBrandName(brandName);
        member.setBusinessType(businessType);
        member.setCategoryId(categoryId);
        member.setCategoryName(categoryName);
        member.setParentId(parent.getId());
        member.setParentName(parent.getName());
        member.setMobileName(mobileName);
        member.setMobile(mobile);
        member.setPhone(phone);
        member.setUserType(userType);
        productService.insert(product);
        super.save(member);
        permissionService.savePermission(member.getId(),null,product.getId(),1,false);
    }

    *//**
     * 修改密码
     * @param phone
     * @param password
     * @param code
     *//*
    public void changePassword(String phone,String password,String code){
        Map<String,Object> param = new HashMap<String, Object>();
        SundayMember member=null;
        codeService.checkValidateCode(phone, code);
        param.clear();
        param.put("phone", phone);
        param.put("isDeleted", SundayCommonConstants.NO);
        List<SundayMember> members=super.list(param);
        if(ListUtil.isNotNull(members)){
            member=members.get(0);
            member.setPassword(Md5UtilJx.GetMD5Code(password));
            super.updateExceptEmpty(member.getId(),member);
        }else {
            throw  new ErrorException("该手机号未注册");
        }

    }
    *//**
     * 前端登录
     * @param phone
     * @param password
     * @return
     *//*
    public SundayMember login(Integer loginType,String userName,String phone,String password){
        Map<String,Object> param = new HashMap<String, Object>();
        if(loginType==1){
            //1,校验用户名
            param.put("phone", phone);
            param.put("isDeleted", SundayCommonConstants.NO);
            if(ListUtil.isNull(super.list(param))){
                throw new ErrorException("手机号未注册");
            }
            param.clear();
            param.put("phone", phone);
            param.put("password", Md5UtilJx.GetMD5Code(password));
            param.put("isDeleted", SundayCommonConstants.NO);
            List<SundayMember> oldMembers = super.list(param);
            if(ListUtil.isNull(oldMembers)){
                throw new ErrorException("密码错误或用户被注销");
            }else{
                //3，组装结果
                return oldMembers.get(0);
            }
        }else if(loginType==2){
            //1,校验用户名
            param.put("userName", userName);
            param.put("isDeleted", SundayCommonConstants.NO);
            if(ListUtil.isNull(super.list(param))){
                throw new ErrorException("不存在的用户名");
            }
            param.clear();
            param.put("userName", userName);
            param.put("password", Md5UtilJx.GetMD5Code(password));
            param.put("isDeleted", SundayCommonConstants.NO);
            List<SundayMember> oldMembers = super.list(param);
            if(ListUtil.isNull(oldMembers)){
                throw new ErrorException("密码错误或用户被注销");
            }else{
                //3，组装结果
                return oldMembers.get(0);
            }
        }
      return null;
    }
    *//**
     *
     * @param phone
     * @param memberId
     *//*
    public void checkMemberIsExist(String phone,String userName,Long memberId){
        Map<String,Object> param = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(phone)){
            param.put("phone", phone);
            param.put("isDeleted", SundayCommonConstants.NO);
            if(LongUtil.isNotZreo(memberId)){
                param.put("id !=", memberId);
            }
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("该手机号已经注册");
            }
        }
        if(StringUtils.isNotBlank(userName)){
            param.put("userName", userName);
            param.put("isDeleted", SundayCommonConstants.NO);
            if(LongUtil.isNotZreo(memberId)){
                param.put("id !=", memberId);
            }
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("该用户名已经注册");
            }
        }

    }

    @Transactional
    public void updateDataBusiness(Long businessId,Long productId,
                           //企业部分
                           String mobileName,String phone,String mobile,String email,String qqNumber,String faxNumber,String englishName,String businessSite,String businessType,String generateTime,String corporation,String amount,
                           Long provinceId,String provinceName,Long cityId,String cityName,Long districtId,String districtName,String address,
                           String creditNum,String licenseNum,String taxNum,String institutionalNum,
                           String brandName,String brandEnglishName,String brandLogo,String brandSource,String brandGenerateTime,String brandRegisterNum,
                            //项目部分
                           String productName,String productViceName,String keyWord,String desc,
                           String investAmount,String joinModel,String joinArea,Integer isAreaAuthorization,Integer directShopNum,Integer joinShopNum,Integer isBusinessBack,String products,
                           String managerModel,String fitPerson,String image
                           ){
                        //企业部分
                        SundayMember business=super.getById(businessId);
                        if(business.getStatus()==SundayCommonConstants.user_status_new||business.getStatus()==SundayCommonConstants.user_status_dsh){
                            business.setStatus(SundayCommonConstants.user_status_dsh);
                        }
                        business.setMobileName(mobileName);business.setPhone(phone);business.setMobile(mobile);business.setEmail(email);business.setQqNumber(qqNumber);business.setFaxNumber(faxNumber);business.setEnglishName(englishName);business.setBusinessSite(businessSite);business.setBusinessType(businessType);business.setGenerateTime(generateTime);business.setCorporation(corporation);
                        business.setAmount(amount);business.setProvinceId(provinceId);business.setProvinceName(provinceName);business.setCityId(cityId);business.setCityName(cityName);business.setDistrictId(districtId);business.setDistrictName(districtName);business.setAddress(address);
                        business.setCreditNum(creditNum);business.setLicenseNum(licenseNum);business.setTaxNum(taxNum);business.setInstitutionalNum(institutionalNum);
                        business.setBrandName(brandName);business.setBrandEnglishName(brandEnglishName);business.setBrandLogo(brandLogo);business.setBrandSource(brandSource);business.setBrandGenerateTime(brandGenerateTime);business.setBrandRegisterNum(brandRegisterNum);
                        super.updateExceptEmpty(businessId,business);
                        //项目部分
                        SundayProduct product=productService.find(productId);
                        if(product.getStatus()==SundayCommonConstants.product_status_new||product.getStatus()==SundayCommonConstants.product_status_dsh){
                            product.setStatus(SundayCommonConstants.product_status_dsh);
                        }
                        product.setName(productName);product.setViceName(productViceName);product.setKeyWord(keyWord);product.setDesc(desc);
                        product.setInvestAmount(investAmount);product.setJoinModel(joinModel);product.setJoinArea(joinArea);product.setBrandSource(brandSource);product.setIsAreaAuthorization(isAreaAuthorization);product.setDirectShopNum(directShopNum);product.setJoinShopNum(joinShopNum);product.setIsBusinessBack(isBusinessBack);product.setProducts(products);
                        product.setManagerModel(managerModel);product.setFitPerson(fitPerson);product.setImage(image);
                        productService.update(product);

    }

    *//**
     * 企业资质审核相关内容
     * @param businessId
     * @param licenseImg
     * @param institutionalImg
     * @param taxImg
     * @param brandRegisterImg
     * @param businessBackImg
     * @param patentImg
     *//*
    public void  updateDataBusiness(Long businessId,String licenseImg,String institutionalImg,String taxImg,String  brandRegisterImg,String businessBackImg,String patentImg){
            SundayMember business=super.getById(businessId);
            if(StringUtils.isNotBlank(licenseImg)){
                business.setLicenseImg(licenseImg);
            }
            if(StringUtils.isNotBlank(institutionalImg)){
                business.setInstitutionalImg(institutionalImg);
            }
            if(StringUtils.isNotBlank(taxImg)){
                business.setTaxImg(taxImg);
            }
            if(StringUtils.isNotBlank(brandRegisterImg)){
                business.setBrandRegisterImg(brandRegisterImg);
            }
            if(StringUtils.isNotBlank(businessBackImg)){
                business.setBusinessBackImg(businessBackImg);
            }
            if(StringUtils.isNotBlank(patentImg)){
                business.setPatentImg(patentImg);
            }
            super.updateExceptEmpty(businessId,business);

    }

    *//**
     * 个人用户修改个人基础信息
     * @param memberId
     * @param nickName
     * @param sex
     * @param marriage
     * @param birthDay
     * @param idCard
     * @param desc
     *//*
    public void updateDataMember(Long memberId,String nickName,String sex,String marriage,String phone,String birthDay,String idCard,String desc,String selfAmount){
            SundayMember member=super.getById(memberId);
            member.setNickName(nickName);
            member.setSex(sex);
            member.setMarriage(marriage);
            member.setPhone(phone);
            member.setBirthDay(birthDay);
            member.setIdCard(idCard);
            member.setDesc(desc);
            member.setSelfAmount(selfAmount);
            super.updateExceptEmpty(memberId,member);
    }*/

    /**
     * 获取主页需要的品牌信息
     * @return
     */
    public Map<String,Object> selectIndexMember(){
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        //加载热门品牌
        param.put("isDeleted",0);
        param.put("isRmpp",1);
        List<SundayMember> rmpps=super.list(param,18);
        result.put("rmpps",rmpps);
        //加载人气品牌
        param.clear();
        param.put("isDeleted",0);
        param.put("isRqpp",1);
        List<SundayMember> rqpps=super.list(param,18);
        result.put("rqpps",rqpps);
        //加载潜力品牌
        param.clear();
        param.put("isDeleted",0);
        param.put("isQlpp",1);
        List<SundayMember> qlpps=super.list(param,18);
        result.put("qlpps",qlpps);

        //加载精品品牌
        param.clear();
        param.put("isDeleted",0);
        param.put("isJppp",1);
        List<SundayMember> jppps=super.list(param,35);
        result.put("jppps",jppps);
        return result;

    }

    /**
     * 获取子行业页面需要的品牌信息
     * @param parentId
     * @param secondId
     * @return
     */
    public Map<String,Object> selectSecondMember(Long parentId,Long secondId){
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        List<SundayMember> brandRecommendMembers=null;
        List<SundayMember> brandTextMembers=null;
        //加载行业品牌推荐
        param.put("isDeleted",0);
        if(LongUtil.isNotZreo(parentId)){
            param.put("parentId",parentId);
            param.put("isBrandRecommend",1);
            brandRecommendMembers=super.list(param,20);
            param.clear();
            param.put("parentId",parentId);
            param.put("isBrandText",1);
            brandTextMembers=super.list(param,3);
        }
        if(LongUtil.isNotZreo(secondId)){
            param.put("categoryId",secondId);
            param.put("isBrandRecommend",1);
            brandRecommendMembers=super.list(param,20);
            param.clear();
            param.put("categoryId",secondId);
            param.put("isBrandText",1);
            brandTextMembers=super.list(param,3);
        }
        result.put("brandRecommendMembers",brandRecommendMembers);
        result.put("brandTextMembers",brandTextMembers);
        return result;
    }

    /**
     * 据记录id返回用户
     * @param memberRecordId
     * @return
     */
    public Long getIdByRecord(Long memberRecordId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("recordId",memberRecordId);
        List<SundayMember> members=super.list(param);
        if(ListUtil.isNotNull(members)){
            return members.get(0).getId();
        }else {
            return 0l;
        }
    }
}
